// pages/book/detail/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active:0,
        isDesk:false,  //是否被加入了书桌
        deskid:''   //记录书桌id
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 渲染详情
        wx.getStorage({
            key:'bookdetail',
            success:(res)=>{
                console.log(res);
                let {sourceUrl} = res.data
                let uuid = sourceUrl.split('/source/')[1]
                //获取目录
                wx.request({
                  url: `https://m.yuedu.163.com/reader/book/info.json?source_uuid=${uuid}&catalog_only=true`,
                  success:(response)=>{
                    console.log(response);
                    let {catalog} = response.data.data
                      this.setData({
                          ...res.data,
                          catalog
                      })
                  }
                })
                // console.log(333);
                // 初始化书桌状态
                let bookid = res.data.objectId
                let {userInfo} = getApp().globalData
                let userid = userInfo?userInfo.userid:''
                let wh = JSON.stringify({userid,bookid})
                getApp().fly.get(`/classes/bookdesk?where=${wh}`).then(res=>{
                    console.log(res);
                    let {results} = res.data
                    if(results.length){
                        this.setData({
                            isDesk:true,
                            deskid:results[0].objectId
                        })
                    }
                })
                // console.log(444);
            }
        })

        
    },
    onChange(ev){
        console.log(ev);
        this.setData({
            active:ev.detail.index
        })
        console.log(this.data.active);
    },
    handleDeskAdd(){ //加入书桌
        let {userInfo} = getApp().globalData
        if(!userInfo){
            getApp().handleLogin()
            return
        }
        //加入书桌 （图书id、用户id、封面、书名）
        let {iconUrl,title} = this.data
        let bookid = this.data.objectId
        let userid = userInfo.userid
        getApp().fly.post('/classes/bookdesk',{
            bookid,
            userid,
            iconUrl,
            title
        }).then(res=>{
            this.setData({
                isDesk:true,
                deskid:res.data.objectId
            })
        })
    },
    handleDeskDel(){
        let {deskid} = this.data
        getApp().fly.delete(`/classes/bookdesk/${deskid}`).then(res=>{
            console.log(res);
            this.setData({isDesk:false})
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})